﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using NotifyServices.Properties;

namespace NotifyServices
{
    public partial class frmServiceManager : Form
    {
        public List<string> ServiceList = new List<string> { "MSSQLSERVER", "MSSQL$SQLEXPRESS", "MSSQL$SQL2008","SQLAgent$SQL2008","SPOOLER" };
       
        public frmServiceManager()
        {
            InitializeComponent();

            LoadServices();

            
        }


        public void LoadServices()
        {
            XElement config = XElement.Load("Config.xml");
            ServiceList = (from service in config.Descendants("services").Elements("service")
                           select service.Value).ToList<string>();
            panelServices.Controls.Clear();
            ServiceList.ForEach(s => LoadControl(s, panelServices));
            notifyIcon1.Visible = true;
        }
       

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
           
            notifyIcon1.Visible = true;
            this.Visible = false;
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.LoadServices();
            notifyIcon1.Visible = false;
        }


        public void LoadControl(string ServiceName, Control container)
        {
            //create a controller button to manage service status
            ServiceControllerButton btnAction = new ServiceControllerButton(".", ServiceName);
            //create a display label for information
            Label lblInfo = new Label() { 
                TextAlign = ContentAlignment.MiddleLeft,
                Text = btnAction.Service.Name };
            //create a panel to host button+label
            Panel pnl = new Panel();
            
            //set up display
            btnAction.Dock = DockStyle.Left;
            btnAction.FlatStyle = FlatStyle.Flat;
            btnAction.FlatAppearance.BorderSize = 0;
            lblInfo.Dock = DockStyle.Fill;
            pnl.Dock = DockStyle.Top;
            //set up events
            btnAction.Click += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    (sender as ServiceControllerButton).UpdateService();
                });
            btnAction.Finished += new ServiceControllerButton.OnFinish(
                delegate(ServiceResponse r)
                {
                    lblInfo.Text = r.Name + ":" + r.CurrentStatus;
                
                });
            
            //then build controls hierarchy
            pnl.Controls.Add(lblInfo);
            pnl.Controls.Add(btnAction);
            pnl.Height = 40;
            
            container.Controls.Add(pnl);
            
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            EditConfig config = new EditConfig();
            config.ShowDialog();
            config.Dispose();
        }
        
        
    }
}
